home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / comparray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-06  |  3.3 KB  |  141 lines

  1. /* 
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. /*******************************************************************/
  28. /*                                                                 */
  29. /* Compressed Arrays                                               */
  30. /*                                                                 */
  31. /*******************************************************************/
  32.  
  33. #include "cf.defs.h"
  34. #include "cf.extern.h"
  35.  
  36. /*******************************************************************/
  37.  
  38. FixCompressedArrayValue(i,value,start)
  39.  
  40. int i;
  41. char *value;
  42. struct CompressedArray **start;
  43.  
  44. { struct CompressedArray *ap;
  45.   char *sp;
  46.  
  47. for (ap = *start; ap != NULL; ap = ap->next)
  48.    {
  49.    if (ap->key == i) 
  50.       {
  51.       /* value already fixed */
  52.       return false;
  53.       }
  54.    }
  55.  
  56. Debug("FixCompressedArrayValue(%d,%s)\n",i,value);
  57.  
  58. if ((ap = (struct CompressedArray *)malloc(sizeof(struct CompressedArray))) == NULL)
  59.    {
  60.    CfLog(cferror,"Can't allocate memory in SetCompressedArray()","malloc");
  61.    FatalError("");
  62.    }
  63.  
  64. if ((sp = malloc(strlen(value)+2)) == NULL)
  65.    {
  66.    CfLog(cferror,"Can't allocate memory in SetCompressedArray()","malloc");
  67.    FatalError("");
  68.    }
  69.  
  70. strcpy(sp,value);
  71. ap->key = i;
  72. ap->value = sp;
  73. ap->next = *start;
  74. *start = ap;
  75. return true;
  76. }
  77.  
  78.  
  79. /*******************************************************************/
  80.  
  81. DeleteCompressedArray(start)
  82.  
  83. struct CompressedArray *start;
  84.  
  85. {
  86. if (start != NULL)
  87.    {
  88.    DeleteCompressedArray(start->next);
  89.    start->next = NULL;
  90.  
  91.    if (start->value != NULL)
  92.       {
  93.       free(start->value);
  94.       }
  95.  
  96.    free(start);
  97.    }
  98. }
  99.  
  100. /*******************************************************************/
  101.  
  102. CompressedArrayElementExists(start,key)
  103.  
  104. struct CompressedArray *start;
  105. int key;
  106.  
  107. { struct CompressedArray *ap;
  108.  
  109. Debug("CompressedArrayElementExists(%d)\n",key);
  110.  
  111. for (ap = start; ap !=NULL; ap = ap->next)
  112.    {
  113.    if (ap->key == key)
  114.       {
  115.       return true;
  116.       }
  117.    }
  118.  
  119. return false;
  120. }
  121.  
  122. /*******************************************************************/
  123.  
  124. char *CompressedArrayValue(start,key)
  125.  
  126. struct CompressedArray *start;
  127. int key;
  128.  
  129. { struct CompressedArray *ap;
  130.  
  131. for (ap = start; ap != NULL; ap = ap->next)
  132.    {
  133.    if (ap->key == key)
  134.       {
  135.       return ap->value;
  136.       }
  137.    }
  138.  
  139. return NULL;
  140. }
  141.